<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Performs a series of post-controller constructor activities
 *
 * @return boolean
 */

function app_takeoff()
{
	//
	// Do everything below on every pageload
	//

	//Load site_config
	global $site_config;

	//Load CI
	$ci =& get_instance();

	$ci->load->library('dbsync');

	if ($ci->dbsync->check_schema_exists())
	{
		$num_diffs = $db_sync_check = $ci->dbsync->get_total_number_of_differences();

		if ($num_diffs > 0)
		{
			if (is_mode('development'))
			{
				if ( ! isset($site_config->enable_devtools) OR ! $site_config->enable_devtools)
					set_notice("Your database is out of sync with your db_schema file!  Enable devtools to see the differences and sync the database.");
			}
			elseif (is_mode('dbsync'))
			{
				$sync_diffs = $ci->dbsync->get_db_schema_changes();
				$queries = array();
				foreach($sync_diffs as $diff)
					$queries[] = $diff->sql_query;

				show_error("<em>Note: You may have to refresh this page several times and run all queries that show up.</em><br /><br /><span style='font-weight: bold; font-size: 1.2em'>The database is out of sync with the file schema! Queries to Run:</span><br /><br />" . implode("<br />", $queries));
			}
			else
				show_error("The database is out of sync with the db_schema file!  Use <em>dbsync</em> site mode to correct the issue.");
		}
	}


	//
	// Do everything below only once per session
	//
	if (session_id() == '')
		session_start();

	if (isset($_SESSION['app_takeoff_pass']) && $_SESSION['app_takeoff_pass'] == 'pass')
		return TRUE;

	//once per session stuff

	//Woo hoo.. the check passed!
	$_SESSION['app_takeoff_pass'] = 'pass';
	return TRUE;
}

/* EOF */